﻿using System;
using System.Net;
using System.Net.Mail;

namespace DotNetStar.TheKing.Infrastructure
{
    /// <summary>
    /// 表示使用邮件的通知。
    /// </summary>
    public class EmailNotification : NotificationBase
    {
        /// <summary>
        /// 初始化 <see cref="EmailNotification"/> 类的新实例。
        /// </summary>
        /// <param name="to">接收方邮件地址。</param>
        /// <param name="subject">邮件主题。</param>
        /// <param name="content">邮件正文。</param>
        public EmailNotification(string to,string subject, string content) : base(to, content)
        {
            this.DisplayName = to;
        }

        public string Subject { get; }
        public string DisplayName { get; set; }

        public override void Send()
        {
            SmtpClient client = new SmtpClient("smtpdm.aliyun.com", 80)
            {
                Credentials = new NetworkCredential("official@notify.dotnetstar.club", "ZHOUzihao5627812"),
            };

            var message = new MailMessage(new MailAddress("official@notify.dotnetstar.club", "码农猩球"), 
                new MailAddress(this.To,this.DisplayName))
            {
                Subject = Subject,
                Body = base.Content,
                Sender = new MailAddress("official@notify.dotnetstar.club"),
                IsBodyHtml = true,
            };

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static EmailNotification SendVerifyCode(string to,string code)             
        => new EmailNotification(to, $@"【码农猩球】接收的验证码", $"你好，{to}<br/>您的验证码是 <h3>{code}</h3>" +
            $"</br>若非本人请忽略此邮件。<p>By <a href='www.dotnetstar.club' target='_blank'>码农猩球</a></p>");
    }
}
